<template>
  <div>
    <el-card>
      <div slot="header">
        <span><b>问答列表</b></span>
        <el-button class="base-el-float-right" type="primary" icon="el-icon-plus" @click="handleEdit(false)">新增问答</el-button>
      </div>

      <PaginationTable :show-pagination="false" :table-data="fileData">
        <el-table-column prop="question" label="问题" />
        <el-table-column prop="answer" label="回答" />
        <el-table-column label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.create_time | timeFilter }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-link class="el-link--primary" style="margin-right: 20px" @click="handleEdit(scope.row)">编辑</el-link>
            <el-link class="el-link--primary" type="danger" @click="handleSubFileDel(scope.row._id)">删除</el-link>
          </template>
        </el-table-column>
      </PaginationTable>
    </el-card>
    <!-- 主文件 -->
    <el-dialog title="文件" :visible="mainFileDialogVisible" :show-close="false">
      <el-form ref="serviceTypeForm" :model="mainFileForm" label-width="60px">
        <el-form-item label="问题" prop="question" required>
          <el-input v-model="mainFileForm.question" />
        </el-form-item>
        <el-form-item label="回答" prop="answer" required>
          <el-input v-model="mainFileForm.answer" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="actionCancel">取消</el-button>
        <el-button type="primary" @click="saveMainFile">确认</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import PaginationTable from '@/components/PaginationTable'
import { deleteById, insertRecord, queryByCondition, updateById } from '@/api/commoncrud'
import { timestampConvert } from '@/utils/time'
export default {
  name: 'Fileedit',
  components: { PaginationTable },
  filters: {
    timeFilter(value) {
      return timestampConvert('yyyy/MM/dd hh:mm', value * 1000)
    }
  },
  data() {
    return {
      messageId: '',
      fileData: [],
      mainFileDialogVisible: false,
      mainFile: {},
      mainFileForm: {}
    }
  },
  async created() {
    this.messageId = this.$route.query.messageId
    await this.fetchData()
  },
  methods: {
    async fetchData() {
      const fileRes = await queryByCondition('qa', 0, 1000, { sort: { create_time: 1 }})
      this.fileData = fileRes.data.documentList
    },
    handleEdit(data) {
      if (data) {
        this.mainFileForm = { ...data }
      } else {
        this.mainFileForm = {}
      }
      this.mainFileDialogVisible = true
    },
    async saveMainFile() {
      const data = {
        question: this.mainFileForm.question,
        answer: this.mainFileForm.answer
      }
      if (this.mainFileForm._id) {
        await updateById('qa', this.mainFileForm._id, data)
      } else {
        await insertRecord('qa', data)
      }
      this.$message.success('操作成功!')
      await this.fetchData()
      this.actionCancel()
    },
    async handleSubFileDel(fileId) {
      this.$alert(`确认删除?`, '消息提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showCancelButton: true,
        callback: async action => {
          if (action === 'confirm') {
            await deleteById('qa', fileId)
            this.$message.success('操作成功!')
            await this.fetchData()
          }
        }
      })
    },
    actionCancel() {
      this.mainFileForm = {}
      this.mainFile = {}
      this.mainFileDialogVisible = false
      this.subFileForm = {}
      this.subFile = {}
      this.subFileDialogVisible = false
    }
  }
}
</script>

<style scoped>

</style>
